package org.jeecg.modules.utils;

import java.io.*;

/**
 * TODO: 数据备份还原的方法
 *
 * @author Zhang Hao yu
 * @date 2020/12/23 14:14
 */

public class SaveAndLoadUtil {
    /**
     * mysql数据库备份
     *
     * @param savePath 保存sql的目标路径
     * @param fileName sql文件名称
     * @param hostIP ip
     * @param userName username
     * @param passWord password
     * @param databaseName 数据库名称
     */
    public static boolean backup(String savePath, String fileName, String hostIP,
                              String userName, String passWord, String databaseName) {
        try {
            Runtime rt = Runtime.getRuntime();
            System.out.println("备份开始······");

            Process child = rt.exec("mysqldump -h " + hostIP + " -u" + userName
                    + " -p" + passWord + "  " + databaseName);

            InputStream in = child.getInputStream();

            InputStreamReader xx = new InputStreamReader(in, "utf-8");

            String inStr;
            StringBuffer sb = new StringBuffer("");
            String outStr;

            BufferedReader br = new BufferedReader(xx);
            while ((inStr = br.readLine()) != null) {
                sb.append(inStr + "\r\n");
            }
            outStr = sb.toString();

            File saveFile = new File(savePath);
            if (!saveFile.exists()) {
                saveFile.mkdirs();
            }
            FileOutputStream fout = new FileOutputStream(savePath + "/"
                    + fileName + ".sql");
            OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
            writer.write(outStr);
            writer.flush();
            in.close();
            xx.close();
            br.close();
            writer.close();
            fout.close();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    /**
     * Mysql数据库的还原
     *
     * @param fPath 还原sql的路径
     * @param hostIP ip
     * @param userName username
     * @param passWord password
     * @param databaseName 数据库名称
     */
    public static String load(String fPath, String hostIP, String userName,
                            String passWord, String databaseName) {
        try {
            System.out.println("还原开始·······");
            // String fPath = "c:/test.sql";

            Runtime rt = Runtime.getRuntime();

            // 调用mysql的cmd命令
            Process child = rt.exec("mysql -u" + userName + " -p" + passWord
                    + "  " + databaseName);
            OutputStream out = child.getOutputStream();
            String inStr;
            StringBuffer sb = new StringBuffer("");
            String outStr;
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    new FileInputStream(fPath), "utf8"));
            while ((inStr = br.readLine()) != null) {
                sb.append(inStr + "\r\n");
            }
            outStr = sb.toString();

            OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
            writer.write(outStr);

            writer.flush();

            out.close();
            br.close();
            writer.close();

            return "还原成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "还原失败";
        }

    }


    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

}